package com.jn.algorithm.day017.question49;

import java.util.*;

/**
 * @author 江南大学1033190417
 * @date 2022/5/8 16:25
 */
public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String t = new String(chars);
            if (map.containsKey(t)) {
                map.get(t).add(s);
            } else {
                List<String> list = new ArrayList<>();
                list.add(s);
                map.put(t, list);
            }
        }
        return new ArrayList<>(map.values());
    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] strs = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(solution.groupAnagrams(strs));
    }
}
